namespace Launcher.Ui

import System
import System.Drawing
import System.Windows.Forms

import Launcher
import Launcher.Model
import Launcher.View
import Launcher.View.Model

class IconButton(UserControl):
"""Description of IconButton"""

	private components as System.ComponentModel.IContainer

	TargetItem:
		get:
			return _item
		set:
			_item = value
			_tooltip.SetToolTip(self, SplitText(_item.TooltipText, 25))
			Invalidate()
	
	private _item as Launcher.View.Model.LauncherItem

	private _distanceFraction as System.Double

	private _parentView as Launcher.View.LauncherView

	_tooltip as ToolTip

	def constructor(parentView as LauncherView):
		InitializeComponent()

		_distanceFraction = 1.0		
		_parentView = parentView
		_tooltip = ToolTip(IsBalloon: true)

		self.BackColor = Constants.ControlColor
		self.Paint += ControlPaint
		
	protected override def Dispose(disposing as bool) as void:
		if disposing:
			if components is not null:
				components.Dispose()
		super(disposing)
		
	private def InitializeComponent() as void:
		self.SuspendLayout()
		# 
		# IconButton
		# 
		self.AutoScaleDimensions = System.Drawing.SizeF(6, 13)
		self.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
		self.Name = "IconButton"
		self.MouseMove += self.IconButtonMouseMove as System.Windows.Forms.MouseEventHandler
		self.MouseClick += self.IconButtonMouseClick as System.Windows.Forms.MouseEventHandler
		self.MouseLeave += self.IconButtonMouseLeave as System.EventHandler
		self.ResumeLayout(false)

	private def ControlPaint(sender as object, e as System.Windows.Forms.PaintEventArgs):
		b = System.Drawing.SolidBrush(Constants.ControlColor)
		e.Graphics.FillRectangle(b, 0, 0, Size.Width, Size.Height)
		DrawIcon(e)
		
	private def DrawIcon(e as System.Windows.Forms.PaintEventArgs):
		maxSize = Size.Width
		minSize = maxSize * 0.66

		size = Interpolate(maxSize, minSize, _distanceFraction)
		x = (Size.Width - size) / 2
		y = (Size.Height - size) / 2
		
		e.Graphics.DrawIcon(_item.Icon, Rectangle(x, y, size, size))
	
	private def IconButtonMouseMove(sender as object, e as System.Windows.Forms.MouseEventArgs):
		distance = Distance(e.X, e.Y, Size.Width / 2, Size.Height / 2)
		totalDistance = Distance(0, 0, Size.Width / 2, Size.Height / 2)

		_distanceFraction = cast(single, distance) / totalDistance
		Invalidate()
	
	private def IconButtonMouseLeave(sender as object, e as System.EventArgs):
		_distanceFraction = 1.0
		Invalidate()
	
	private def IconButtonMouseClick(sender as object, e as System.Windows.Forms.MouseEventArgs):
		_parentView.ItemSelected(self, _item)
